<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Mybatis代码自动生成</title>

    <!-- Bootstrap core CSS -->
    <link href="../../static/css/bootstrap.min.css" rel="stylesheet" th:href="@{/css/bootstrap.min.css}"/>
    <link rel="stylesheet"
          href="../../static/css/bootstrap-select.min.css" th:href="@{/css/bootstrap-select.min.css}"/>
    <!-- Animation CSS -->
    <link href="../../static/css/font-awesome.css" rel="stylesheet" th:href="@{/css/font-awesome.css}"/>

    <!-- Custom styles for this template -->
    <link href="../../static/css/style.css" rel="stylesheet" th:href="@{/css/style.css}"/>
    <!-- JS-->
    <script src="../../static/js/jquery-2.1.1.js" th:src="@{/js/jquery-2.1.1.js}"></script>
    <script src="../../static/js/bootstrap.min.js" th:src="@{/js/bootstrap.min.js}"></script>
    <script src="../../static/js/bootstrap-select.min.js" th:src="@{/js/bootstrap-select.min.js}"></script>
    <script src="../../static/js/defaults-zh_CN.min.js" th:src="@{/js/defaults-zh_CN.min.js}"></script>
</head>
<body id="page-top" class="landing-page no-skin-config">
<section id="pricing" class="pricing">
    <div class="container">
        <div class="row m-b-lg">
            <div class="col-lg-12 text-center">
                <div class="ibox-content">
                    <a class="btn btn-primary btn-rounded btn-block" onclick="getTablesName();"
                       style="'margin-left:'+ '-10px'">自动生成</a>
                </div>
                <div class="navy-line">
                </div>
                <h1>Mybatis自动代码生成工具</h1>
                <p>可根据表名与表中字段选择性生成</p>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-4 wow zoomIn">
                <ul class="pricing-plan list-unstyled">
                    <li class="pricing-title">
                        表名
                    </li>
                    <li>
                        <div class="btn-group">
                            <select id="tablename" onchange="getTables();" name="tablename"
                                    class="selectpicker show-tick form-control" multiple data-live-search="false"
                                    data-actions-box="true" data-width="auto">

                            </select>
                        </div>
                    </li>
                </ul>
            </div>

            <div class="col-lg-4 wow zoomIn">
                <ul class="pricing-plan list-unstyled selected">
                    <li class="pricing-title">
                        表中字段
                    </li>
                    <li>
                        <div class="btn-group">
                            <select id="columnsname" onchange="getColumns();" name="columnsname"
                                    class="selectpicker show-tick form-control" multiple data-live-search="false"
                                    data-width="auto" data-actions-box="true" title="单表可选,多表默认为所有字段">

                            </select>
                        </div>
                    </li>
                </ul>
            </div>

            <div class="col-lg-4 wow zoomIn">
                <ul class="pricing-plan list-unstyled">
                    <li class="pricing-title">
                        选择生成需求
                    </li>
                    <li>
                        <div class="btn-group">
                            <select id="require" name="require" onchange="getRequire();"
                                    class="selectpicker show-tick form-control" multiple data-live-search="false"
                                    data-width="auto" data-actions-box="true">
                                <option value="Dao">Dao</option>
                                <option value="Mapper">Mapper</option>
                                <option value="Entity">Entity</option>
                                <option value="Service">Service</option>
                                <option value="Action">Action</option>
                            </select>
                        </div>
                    </li>
                </ul>
            </div>

        </div>
    </div>
</section>

<script>

    function selectTables() {
        $.ajax({
            url: "http://localhost:8090/generator/getTables",
            type: "post",
            contentType: "application/json",
            traditional: true,
            success: function (data) {
                var content = '';

                $.each(data, function (index, v) {
                    content += ' <option value="' + v + '">' + v + '</option>';
                });
                $("#tablename").append(content);
                <!-- 下列2行必须有-->
                $('#tablename').selectpicker('refresh');
                $('#tablename').selectpicker('render');
            }
        });

    }

    selectTables();

    function getTables() {
        if ($('#tablename').selectpicker('val')) {
            $.ajax({
                url: "http://localhost:8090/generator/getColumns",
                type: "post",
                traditional: true,
                data: {
                    tablesName: $('#tablename').selectpicker('val')
                },
                success:
                    function (data) {
                        if ($('#tablename').selectpicker('val').length > 1) {
                            $('#columnsname').prop('disabled', true);
                        } else {
                            $('#columnsname').prop('disabled', false);

                        }
                        $('#columnsname').empty();
                        var content = '';
                        for (x in data) {
                            for (y in data[x]) {
                                content += ' <option value="' + data[x][y].column_name + '">' + data[x][y].column_name + '</option>';
                            }
                        }
                        $('#columnsname').append(content);
                        <!-- 下列2行必须有-->
                        $('#columnsname').selectpicker('refresh');
                        $('#columnsname').selectpicker('render');
                    }
            });

        }
    }

    function getColumns() {
        $.ajax({
            url: "http://localhost:8090/generator/getColumns",
            type: "post",
            traditional: true,
            data: {
                tablesName: $('#tablename').selectpicker('val')
            }
        });
    }

    function getRequire() {
        if ($('#require').selectpicker('val')) {
            $.ajax({
                url: "http://localhost:8090/generator/getRequire",
                type: "post",
                traditional: true,
                data: {
                    require: $('#require').selectpicker('val')
                }
            });
        }

    }

    function getTablesName() {
        if ($('#tablename').selectpicker('val') != null &&
            $('#require').selectpicker('val') != null) {
            if ($('#tablename').selectpicker('val').length > 1) {
                $.ajax({
                    url: "http://localhost:8090/generator/getParameterToGenerator",
                    type: "post",
                    traditional: true,
                    data: {
                        tablesName: $('#tablename').selectpicker('val'),
                        require: $('#require').selectpicker('val')
                    },
                    success: function () {
                        $.ajax()
                    }
                });
            } else {
                if ($('#columnsname').selectpicker('val') != null) {
                    $.ajax({
                        url: "http://localhost:8090/generator/getParameterToGenerator",
                        type: "post",
                        traditional: true,
                        data: {
                            tablesName: $('#tablename').selectpicker('val'),
                            columns: $('#columnsname').selectpicker('val'),
                            require: $('#require').selectpicker('val')
                        },
                        success: function () {

                            var inputObj=document.createElement('input');
                            inputObj.setAttribute('id','_ef');
                            inputObj.setAttribute('type','file');
                            inputObj.setAttribute("style",'visibility:hidden');
                            document.body.appendChild(inputObj);
                            inputObj.click();
                            inputObj.value ;
                        }
                    });
                }
            }

        }
    }

</script>


</body>
</html>
